//
//  HJChatMessageTextCell.m
//  XWC
//
//  Created by Hello on 2022/10/25.
//

#import "HJChatMessageTextCell.h"
#import "HJChatMessageTextLayout.h"
#import "HJChatMessageModel.h"
#import "QMUIButton.h"
#import "YYLabel.h"
#import "HJLinkPreviewView.h"

@interface HJChatMessageTextCell()

@property (nonatomic, strong) HJChatMessageTextLayout *textLayout;
@property (nonatomic, weak) QMUIButton *foldButton;
@property (nonatomic, weak) HJLinkPreviewView *linkPreviewView;

@end


@implementation HJChatMessageTextCell 


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        YYLabel *contentLabel = [YYLabel addToView:self.bubbleView];
        contentLabel.textAlignment = NSTextAlignmentLeft;
        contentLabel.numberOfLines = 0;
        contentLabel.textColor = UIColor.tj_333333Color;
        contentLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
        contentLabel.font = UIFontMake(14);
        _contentLabel = contentLabel;
                
        QMUIButton *foldButton = [QMUIButton buttonWithSuperView:self.bubbleView font:UIFontMake(14) color:UIColor.tj_mainColor title:nil];
        [foldButton setImagePosition:QMUIButtonImagePositionRight];
        [foldButton setImage:[UIImage imageNamed:@"message_top"] forState:UIControlStateNormal];
        [foldButton setImage:[UIImage imageNamed:@"message_down"] forState:UIControlStateSelected];
        [foldButton setTitle:@"Hide" forState:UIControlStateNormal];
        [foldButton setTitle:@"Expand" forState:UIControlStateSelected];
        [foldButton setSpacingBetweenImageAndTitle:4];
        [foldButton addTarget:self action:@selector(didClickFoldButton:) forControlEvents:UIControlEventTouchUpInside];
        _foldButton = foldButton;
        
        HJLinkPreviewView *linkPreviewView = [HJLinkPreviewView addToView:self.bubbleView];
        [linkPreviewView addTapGesturesWithTarget:self action:@selector(tapLinkPreviewView)];
        linkPreviewView.addCornerRadius(5);
        _linkPreviewView = linkPreviewView;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _contentLabel.frame = self.textLayout.contentRect;
    if (self.textLayout.foldRect.size.height > 0) {
        [_foldButton sizeToFit];
        _foldButton.frame = CGRectMake(self.textLayout.contentRect.origin.x, CGRectGetMaxY(self.textLayout.contentRect), _foldButton.bounds.size.width, self.textLayout.foldRect.size.height);
    }  
    _linkPreviewView.frame = self.textLayout.linkPreviewRect;
}

- (void)setMessageLayout:(HJChatMessageLayout *)messageLayout {
    [super setMessageLayout:messageLayout];
    
    self.textLayout = (HJChatMessageTextLayout *)messageLayout;
    self.contentLabel.font = self.textLayout.textFont;
    self.contentLabel.attributedText = self.textLayout.attributedString;
    
    if (self.textLayout.linkPreviewInfo) {
        if (self.textLayout.linkPreviewIcon) {
            self.linkPreviewView.iconImageView.image = self.textLayout.linkPreviewIcon;
        } else {
            NSString *icon = self.textLayout.linkPreviewInfo[@"icon"];
            UIImageView *iconImageView = self.linkPreviewView.iconImageView;
            TJWeakify(iconImageView);
            [self.linkPreviewView.iconImageView tj_setImageWithURL:[icon URL] placeholderImage:nil completed:^(UIImage * _Nullable image) {
                if (!image || image.size.width == 0) {
                    weakiconImageView.image = [UIImage imageNamed:@"link_icon"];
                }
            }];
        }
 
        self.linkPreviewView.titleLabel.text = self.textLayout.linkPreviewInfo[@"title"];
        self.linkPreviewView.linkLabel.text = self.textLayout.linkPreviewInfo[@"url"];
        self.linkPreviewView.descriptionLabel.attributedText = self.textLayout.linkDescriptionAttributedString;
        self.linkPreviewView.descriptionLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    }

    if (self.textLayout.foldRect.size.height) {
        _foldButton.hidden = NO;
        if (self.textLayout.isFold) {//显示Expand
            _foldButton.selected = YES;
        } else {//显示Hide
            _foldButton.selected = NO;
        }
    } else {
        _foldButton.hidden = YES;
    }
    
    if (self.textLayout.isSelfSend) {
        self.linkPreviewView.backgroundColor = UIColor.whiteColor;
    } else {
        self.linkPreviewView.backgroundColor = UIColor.tj_F6F6F6Color;
    }
    
    [messageLayout.messageModel previewLink];
    
    TJWeakify(self)
    [self.textLayout setDidClickLinkBlock:^(NSString * _Nonnull url) {
        if ([weakself.delegate respondsToSelector:@selector(chatMessageCell:didClickLink:)]) {
            [weakself.delegate chatMessageCell:weakself didClickLink:url];
        }
    }];
}

- (void)didClickFoldButton:(QMUIButton *)button {
    if ([self.delegate respondsToSelector:@selector(chatMessageCell:didClickFoldButton:)]) {
        [self.delegate chatMessageCell:self didClickFoldButton:button];
    }
}

- (void)tapLinkPreviewView {
    if ([self.delegate respondsToSelector:@selector(chatMessageCellDidClickLinkPreviewView:)]) {
        [self.delegate chatMessageCellDidClickLinkPreviewView:self];
    }
}

@end
